package org.linlinjava.litemall.db.service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallAwardMapper;
import org.linlinjava.litemall.db.dao.LitemallAwardPlanMapper;
import org.linlinjava.litemall.db.dao.LitemallAwardStatisticsMapper;
import org.linlinjava.litemall.db.dao.LitemallUserLotteryMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhy
 * @date 2019-07-02 18:15
 **/
@Service
public class LitemallAwardService {
	@Resource
	private LitemallAwardMapper litemallAwardMapper;

	@Resource
	private LitemallAwardStatisticsMapper litemallAwardStatisticsMapper;

	@Resource
	private LitemallUserLotteryMapper litemallUserLotteryMapper;

	@Resource
	private LitemallAwardPlanMapper litemallAwardPlanMapper;

	public int updeteSendFlag(LitemallUserLottery litemallUserLottery) {
		return litemallUserLotteryMapper.updateByPrimaryKeySelective(litemallUserLottery);
	}

	public int getTodayAwardPrice(){
		return litemallAwardMapper.todayAward(LocalDate.now());
	}

	public int getTodayUse() {
		return litemallAwardMapper.todayUse(LocalDate.now());
	}
	public int three() {
		LitemallUserLotteryExample example = new LitemallUserLotteryExample();
		example.or().andDrawTimeBetween(LocalDate.now().minusDays(3),LocalDate.now());
		int num = (int)litemallUserLotteryMapper.countByExample(example);
		return num*10;
	}

	public int allNum() {
		return litemallAwardMapper.allNum(LocalDate.now());
	}

	public int leftNum() {
		return litemallAwardMapper.leftNum(LocalDate.now());
	}

	public List<LitemallUserLottery> getlotteryList(LocalDate start,LocalDate end,Integer page, Integer size,short sendFlag ){
		LitemallUserLotteryExample example = new LitemallUserLotteryExample();
		LitemallUserLotteryExample.Criteria criteria = example.createCriteria();
		if(start!=null && end!=null){
			criteria.andDrawTimeGreaterThanOrEqualTo(start).andDrawTimeLessThanOrEqualTo(end);
		}
		if (sendFlag!=(short)4){
			criteria.andSendFlagEqualTo(sendFlag);
		}
		example.orderBy(LitemallUserLottery.Column.drawTime.desc());
		PageHelper.startPage(page, size);
		return litemallUserLotteryMapper.selectByExample(example);
	}

	public List<LitemallAward> getList(String awardName,Integer page, Integer size){
		LitemallAwardExample example = new LitemallAwardExample();
		LitemallAwardExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(awardName)){
			criteria.andAwardNameLike(awardName);
		}
		PageHelper.startPage(page, size);
		return litemallAwardMapper.selectByExample(example);
	}

	public List<LitemallAwardPlan> getAllIntegeal(){
		LitemallAwardPlanExample example = new LitemallAwardPlanExample();
		example.or().andActiveDateEqualTo(LocalDate.now());
		return litemallAwardPlanMapper.selectByExample(example);
	}

	public List<LitemallAwardPlan> getPlanByAwardId(Integer id){
		LitemallAwardPlanExample example = new LitemallAwardPlanExample();
		example.or().andAwardIdEqualTo(id);
		example.orderBy(LitemallAwardPlan.Column.activeDate.desc());
		return litemallAwardPlanMapper.selectByExample(example);
	}

	public int deletePlanByAwardId(Integer id){
		return litemallAwardPlanMapper.logicalDeleteByPrimaryKey(id);
	}

	public List<LitemallAward> getAwardList(){
		LitemallAwardExample example = new LitemallAwardExample();
		example.or().andStatusEqualTo(true);
		return litemallAwardMapper.selectByExample(example);
	}

	public LitemallAwardStatistics getStatistics(){
		LitemallAwardStatisticsExample example = new LitemallAwardStatisticsExample();
		return litemallAwardStatisticsMapper.selectOneByExample(example);
	}

	public int upAllInteg(LitemallAwardStatistics litemallAwardStatistics){
		return litemallAwardStatisticsMapper.updateByPrimaryKeySelective(litemallAwardStatistics);
	}

	public int addPlan (LitemallAwardPlan litemallAwardPlan){
		return litemallAwardPlanMapper.insertSelective(litemallAwardPlan);
	}
	public int addAward(LitemallAward litemallAward){
		return litemallAwardMapper.insertSelective(litemallAward);
	}

	public int upAward(LitemallAward litemallAward){
		return litemallAwardMapper.updateByPrimaryKeySelective(litemallAward);
	}

	public List<LitemallUserLottery> getUserById(Integer awardId){
		LitemallUserLotteryExample example = new LitemallUserLotteryExample();
		example.or().andAwardIdEqualTo(awardId);
		return litemallUserLotteryMapper.selectByExample(example);
	}
}
